package com.ksy.common.utils;


import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by Ksy
 */

public class CommonApiUtils {

    /**
     * 根据Code获取token
     *
     * @return {
     * "access_token":"ACCESS_TOKEN",
     * "expires_in":7200,
     * "refresh_token":"REFRESH_TOKEN",
     * "openid":"OPENID",
     * "scope":"SCOPE"
     * }
     */


    public static Observable<String> getWeChatToken(String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("appid", Constants.WeChatID);
        map.put("secret", Constants.WeChatSecret);
        map.put("grant_type", "authorization_code");
        map.put("code", code);
        return ApiManager.getApiServer().getWeChatToken("https://api.weixin.qq.com/sns/oauth2/access_token", map);
    }

    /**
     * 根据refresh_token刷新token
     *
     * @return {
     * "access_token":"ACCESS_TOKEN",
     * "expires_in":7200,
     * "refresh_token":"REFRESH_TOKEN",
     * "openid":"OPENID",
     * "scope":"SCOPE"
     * }
     */


    public static Observable<String> refreshToken(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("appid", Constants.WeChatID);
        map.put("refresh_token", token);
        map.put("grant_type", "refresh_token");
        return ApiManager.getApiServer().refreshToken("https://api.weixin.qq.com/sns/oauth2/refresh_token", map);
    }

    /**
     * 检验授权凭证（access_token）是否有效
     *
     * @return {
     * "errcode":0,"errmsg":"ok"
     * }
     */


    public static Observable<String> validToken(String token, String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("openid", openId);
        map.put("access_token", token);
        return ApiManager.getApiServer().validToken("https://api.weixin.qq.com/sns/auth", map);
    }

    /**
     * 根据access_token,openid 获取用户个人信息
     *
     * @return {
     * "openid":"OPENID",
     * "nickname":"NICKNAME",
     * "sex":1,
     * "province":"PROVINCE",
     * "city":"CITY",
     * "country":"COUNTRY",
     * "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
     * "privilege":[
     * "PRIVILEGE1",
     * "PRIVILEGE2"
     * ],
     * "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
     * }
     * }
     */

    public static Observable<String> getWeChatInfo(String token, String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("access_token", token);
        map.put("openid", openId);
        map.put("lang", "zh_CN");
        return ApiManager.getApiServer().getWeChatInfo("https://api.weixin.qq.com/sns/userinfo", map);
    }


    public static Observable<ResponseBody> downloadFile(String url, Observer<ResponseBody> ob) {
        Observable<ResponseBody> observable = ApiManager.getApiServer(ApiServer.class).downloadFile(url);
        observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(ob);
        return observable;
    }

}
